<template>
  <el-container class="home_container">
    <el-header>
      <div><span>电商后台管理系统</span></div>
      <el-button @click="logout" type="info">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <i class="el-icon-more-outline"></i>
        </div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          unique-opened
        >
          <el-submenu
            v-for="menu in menuList"
            :key="menu.id"
            :index="'' + menu.id"
          >
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{ menu.authName }}</span>
            </template>
            <el-menu-item
              v-for="subMenu in menu.children"
              :key="subMenu.id"
              :index="'/' + subMenu.path"
            >
              <i class="el-icon-menu"></i>
              <span slot="title">{{ subMenu.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>Main</el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      isCollapse: false
    }
  },
  methods: {
    // 注销
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单
    async getMenuList() {
      const response = await this.$request.get('menus')
      this.menuList = response.data
    },
    // 侧边栏折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    }
  },
  created() {
    this.getMenuList()
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;

  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #373d41;

    > div {
      //width: 58px;
      height: 58px;
      background: transparent url('../assets/logo-small.png') no-repeat;
      line-height: 58px;
      color: #fff;
      font-size: 20px;

      > span {
        margin-left: 70px;
      }
    }
  }

  .toggle-button {
    background-color: #4a5064;
    font-size: 20px;
    line-height: 20px;
    color: #fff;
    text-align: center;
    cursor: pointer;
  }

  .el-aside {
    background-color: #333744;
    .el-menu {
      border-right: none;
    }
  }
}
</style>
